<template>
  <div class="reportManagement">
    <nav-header></nav-header>
    <div class="top">
      <div class="left">
        <div class="selecter">
          <el-select v-model="examine_status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.examine_status"
              :label="item.label"
              :value="item.examine_status"
            >
            </el-option>
          </el-select>
        </div>
        <el-input
          placeholder="请输入举报人/被举报账号进行搜索"
          v-model="keyword"
          @keyup.enter.native="search"
        ></el-input>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </div>
    </div>
    <div class="zh-table-contents">
      <el-table :data="tableData" border max-height="600" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          show-overflow-tooltip
          width="50"
        >
        </el-table-column>
        <el-table-column
          prop="report_username"
          label="被举报账号"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="report_type_name"
          label="举报类型"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="state"
          label="当前状态"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="create_username"
          label="举报人账号"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="举报时间"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="delete-button"
              @click="del(scope.row)"
              v-if="scope.row.examine_status !== 0"
              >删除</el-button
            >
            <el-button
              type="text"
              @click="agree(scope.row)"
              v-if="scope.row.examine_status === 0"
              >通过</el-button
            >
            <el-button
              type="text"
              @click="refuse(scope.row)"
              v-if="scope.row.examine_status === 0"
              >拒绝</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="table-pagination" v-show="total_number >= 10">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.page_index"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="params.page_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total_number"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      examine_status: null,
      total_number: 0,
      keyword: null,
      options: [
        {
          examine_status: null,
          label: "全部",
        },
        {
          examine_status: 0,
          label: "未审核",
        },
        {
          examine_status: 1,
          label: "已通过",
        },
        {
          examine_status: 2,
          label: "已拒绝",
        },
      ],
      params: {
        examine_status: null,
        keyword: null,
        page_index: 1,
        page_size: 10,
      },
      tableData: [],
    };
  },
  created() {
    this.getReportData();
  },
  methods: {
    reset() {
      this.examine_status = null;
      this.keyword = null;
      this.params = {
        examine_status: null,
        keyword: null,
        page_index: 1,
        page_size: 10,
      };
      this.getReportData();
    },
    search() {
      this.params.examine_status = this.examine_status;
      this.params.keyword = this.keyword;
      this.getReportData();
    },
    handleSizeChange(val) {
      this.params.page_index = 1;
      this.params.page_size = val;
      this.getReportData();
    },
    handleCurrentChange(val) {
      this.params.page_index = val;
      this.getReportData();
    },
    getReportData() {
      let success = (res) => {
        this.total_number = res.data.data_sum;
        let result = res.data.data;
        result.forEach((item) => {
          if (item.examine_status === 0) item.state = "未审核";
          if (item.examine_status === 1) item.state = "已通过";
          if (item.examine_status === 2) item.state = "已拒绝";
        });
        this.tableData = result;
      };
      let fail = (res) => {
        this.$message.error(`${res.msg}`);
      };
      this.$ajax.get("/appapi/setting/report_data", {
        params: this.params,
        func: { success, fail },
      });
    },
    del(row) {
      this.$confirm("即将删除该举报信息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 网络请求
        this.delApi(row.id);
      });
    },
    delApi(id) {
      let success = (res) => {
        this.$message.success(`${res.msg}`);
        this.getReportData();
      };
      let fail = (res) => {
        this.$message.error(`${res.msg}`);
      };
      this.$ajax.delete("/appapi/setting/report_examine", {
        params: { id },
        func: { success, fail },
      });
    },
    agree(row) {
      this.$confirm(
        `审核通过将注销 ${row.report_username} 的账号，请谨慎操作`,
        "提示",
        {
          confirmButtonText: "通过",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        // 网络请求
        this.agreeApi(row.id);
      });
    },
    agreeApi(id) {
      let success = (res) => {
        this.$message.success(`${res.msg}`);
        this.getReportData();
      };
      let fail = (res) => {
        this.$message.error(`${res.msg}`);
        this.getReportData();
      };
      this.$ajax.post(
        "/appapi/setting/report_examine",
        { id },
        { func: { success, fail } }
      );
    },
    refuse(row) {
      // 网络请求
      let success = (res) => {
        this.$message.success(`${res.msg}`);
        this.getReportData();
      };
      let fail = (res) => {
        this.$message.error(`${res.msg}`);
        this.getReportData();
      };
      this.$ajax.put(
        "/appapi/setting/report_examine",
        { id: row.id },
        { func: { success, fail } }
      );
    },
  },
};
</script>

<style scoped lang='scss'>
.reportManagement {
  .top {
    margin-bottom: 20px;
    .left {
      width: 500px;
      display: flex;
      .selecter {
        width: 200px;
      }
      .el-input {
        margin: 0 10px;
      }
    }
  }
  .tableContent {
  }
  /deep/ .el-table--border th {
    border-right: 0px;
  }
}
</style>
